#!/bin/sh

make || exit 1

echo "Creating memory disk for tests (this will require root privilege)..."
sudo mdconfig -a -t malloc -o reserve -s 1M -n > mdnum.txt
MDNUM=`cat mdnum.txt`
rm mdnum.txt

sudo newfs -U /dev/md${MDNUM} > /dev/null

MOUNTPOINT=`mktemp -d /tmp/openattest.XXXXXX`
sudo mount /dev/md${MDNUM} ${MOUNTPOINT}

PRE_INODES=`df -i | grep ${MOUNTPOINT} | awk '{print $6 }'`

sudo chown -R `whoami` ${MOUNTPOINT}
mkdir -p          ${MOUNTPOINT}/foo/foo1
mkdir -p          ${MOUNTPOINT}/foo/foo2
mkdir             ${MOUNTPOINT}/bar
mkdir             ${MOUNTPOINT}/foo/fubar
mkdir             ${MOUNTPOINT}/bar/fubar
echo "foo1a"  >>  ${MOUNTPOINT}/foo/foo1/foo1a
echo "foo1b"  >>  ${MOUNTPOINT}/foo/foo1/foo1b
echo "bar1"   >>  ${MOUNTPOINT}/bar/bar1
echo "bar2"   >>  ${MOUNTPOINT}/bar/bar2

echo "Running tests..."
echo "===================================================================="
ktrace -di ./test ${MOUNTPOINT}
echo "===================================================================="


rm -rf ${MOUNTPOINT}/*

POST_INODES=`df -i | grep ${MOUNTPOINT} | awk '{print $6 }'`

if [ "${PRE_INODES}" != "${POST_INODES}" ]; then
	echo "Error! Pre- and post-test inode counts differ:"
	echo "Pre-test (empty FS):    ${PRE_INODES}"
	echo "Post-test (empty FS):   ${POST_INODES}"
	echo "Perhaps a vnode's refcount isn't being decremented..."
fi

echo "Cleaning up (this will require root privilege)..."
sudo umount -f ${MOUNTPOINT}
rmdir ${MOUNTPOINT}
sudo mdconfig -d -u ${MDNUM}

echo "Done."

